package com.umakr.demo.controller;

import com.umakr.demo.model.Combobox;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/day12")
@Controller
public class Controller12 {

    @RequestMapping("/Tree")
    public String Tree(){
        return "day12/tree_ajax";
    }

    @RequestMapping("/showTree")
    @ResponseBody
    public List showTree(){
        List<Combobox.treedemo> list_zitree1 = new ArrayList<>();
        Combobox.treedemo z1 = new Combobox.treedemo("111", "Friend");
        Combobox.treedemo z2 = new Combobox.treedemo("112", "Wife");
        Combobox.treedemo z3 = new Combobox.treedemo("113", "Company");
        list_zitree1.add(z1);
        list_zitree1.add(z2);
        list_zitree1.add(z3);

        List<Map> list_ztree = new ArrayList<>();
        Map<String,String> map1 = new HashMap<>();
        map1.put("p1","Custom Attribute1");
        map1.put("p2","Custom Attribute2");
        list_ztree.add(map1);

        List<Combobox.treedemo> list_zitree2 = new ArrayList<>();
        Combobox.treedemo z4 = new Combobox.treedemo("122","Java");
        z4.setAttributes(list_ztree);
        Combobox.treedemo z5 = new Combobox.treedemo("121","Intel");
        Combobox.treedemo z6 = new Combobox.treedemo("123","Microsoft Office");
        Combobox.treedemo z7 = new Combobox.treedemo("124","Games");
        list_zitree2.add(z4);
        list_zitree2.add(z5);
        list_zitree2.add(z6);
        list_zitree2.add(z7);

        List<Combobox.treedemo> list_zitree3 = new ArrayList<>();
        Combobox.treedemo z11 = new Combobox.treedemo("11","Photos",list_zitree1);
        Combobox.treedemo z12 = new Combobox.treedemo("12","Program Files",list_zitree2);
        list_zitree3.add(z11);
        list_zitree3.add(z12);

        List<Combobox.treedemo> list = new ArrayList<>();
        Combobox.treedemo node = new Combobox.treedemo("1","My Documents",list_zitree3);
        list.add(node);
        System.out.println(z4.getAttributes());
        return list;
    }


    @RequestMapping("/form")
    public String form(){
        return "day12/form_ajax";
    }
//  表单初始化的时候加载的数据
    @RequestMapping("/formData")
    @ResponseBody
    public Map formData(){
        Map<String,String> map = new HashMap<>();
        map.put("productid","easyui");
        map.put("productname","easyui@gmail.com");
        map.put("unitcost","10.00");
        map.put("status","p");
        map.put("listprice","36.50");
        map.put("attr1","attr1");
        map.put("itemid","EST-10");
        System.out.println(map);
        return map;
    }
//    表单提交过来的数据
    @RequestMapping("/ajaxData")
    @ResponseBody
    public void ajaxData(String productid, String productname, String unitcost, String status, String listprice, String attr1,String itemid){
        Map<String,String> map = new HashMap<>();
        map.put("productid",productid);
        map.put("productname",productname);
        map.put("unitcost",unitcost);
        map.put("status",status);
        map.put("listprice",listprice);
        map.put("attr1",attr1);
        map.put("itemid",itemid);
    }

    @RequestMapping("/showCombobox")
    public String showCombobox(){
        return "/day12/combobox_ajax";
    }

    @RequestMapping("/comboboxData")
    @ResponseBody
    public void comboboxData(){
//        List<Combobox> list = new ArrayList<>();
//        Combobox c1 = new Combobox();
//        c1.setId(1);
//        c1.setText("Java");
//        c1.setDesc("Write once, run anywhere");
//
//        Combobox c2 = new Combobox();
//        c2.setId(2);
//        c2.setText("C");
//        c2.setDesc("One of the programming languages designed for the Common Language Infrastructure");
//
//        Combobox c3 = new Combobox();
//        c3.setId(3);
//        c3.setText("Ruby");
//        c3.setSelected(true);
//        c3.setDesc("A dynamic, reflective, general-purpose object-oriented programming language");
//
//        Combobox c4 = new Combobox();
//        c4.setId(4);
//        c4.setText("Perl");
//        c4.setDesc("A high-level, general-purpose, interpreted, dynamic programming language");
//
//        list.add(c1);
//        list.add(c2);
//        list.add(c3);
//        list.add(c4);
//
//        System.out.println(list);
//        return  list;list
    }

    @RequestMapping("/table")
    public String table(){
        return "day12/table_ajax" ;
    }


    Combobox.Product p1 = new Combobox.Product("FI-SW-01","Koi","10.00","P","36.50","Large","EST-1");
    Combobox.Product p2 = new Combobox.Product("K9-DL-01","Dalmation","12.00","P","18.50","Spotted Adult Female","EST-10");
    Combobox.Product p3 = new Combobox.Product("RP-SN-01","Rattlesnake","12.00","P","26.50","Rattleless","EST-12");
    Combobox.Product p4 = new Combobox.Product("RP-SN-01","Rattlesnake","12.00","P","38.50","Venomless","EST-11");
    Combobox.Product p5 = new Combobox.Product("FL-DLH-02","Persian","12.00","P","23.50","Adult Female","EST-16");
    List<Combobox.Product> list = new ArrayList<>();

    //获取原始数据
    @RequestMapping("/tableDate")
    @ResponseBody
    public Map tableDate(String productid, String productname, String unitcost, String status, String listprice, String attr1,String itemid){
////       原始数据
//        Product p1 = new Product("FI-SW-01","Koi","10.00","P","36.50","Large","EST-1");
//        Product p2 = new Product("K9-DL-01","Dalmation","12.00","P","18.50","Spotted Adult Female","EST-10");
//        Product p3 = new Product("RP-SN-01","Rattlesnake","12.00","P","26.50","Rattleless","EST-12");
//        Product p4 = new Product("RP-SN-01","Rattlesnake","12.00","P","38.50","Venomless","EST-11");
//        Product p5 = new Product("FL-DLH-02","Persian","12.00","P","23.50","Adult Female","EST-16");

        list.add(p1);
        list.add(p2);
        list.add(p3);
        list.add(p4);
        list.add(p5);

        //处理逻辑
        for(int i=0;i<list.size();i++){
//            if(list.get(i).getProductid().equals(productid)&&itemid !)
        }
//        if(productid != null){
//            for(int i=0;i<list.size();i++){
//                if(list.get(i).getProductid().equals(productid) && itemid != null ){
////                    修改数据
//                    list.get(i).setProductid(productid);
//                    list.get(i).setProductname(productname);
//                    list.get(i).setUnitcost(unitcost);
//                    list.get(i).setStatus(status);
//                    list.get(i).setListprice(listprice);
//                    list.get(i).setAttr1(attr1);
//                }else if(list.get(i).getProductid().equals(productid) && itemid == null){
////                   删除数据
//                    list.remove(i);
//                }
//             }
//
////                  创建数据
//
//                Product p  = new Product(productid,productname,unitcost,status,listprice,attr1,itemid);
//                list.add(p);

//        }




        Map<String,Object> map = new HashMap<>();
        map.put("total",28);
        map.put("rows",list);

        return  map ;
    }


}
